<?php

namespace app\common\model\xiluhouse;

use think\Model;

/**
 * 会员模型
 */
class User extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        // 'is_empty_avatar',
        // 'jointime_text',
    ];
    protected static function init()
    {
        self::beforeDelete(function ($row) {
            UserProfile::where('user_id', $row['id'])->delete();
        });
    }
    public function getJointimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
        return time_tran($value);
    }

    /**
     * 获取头像
     * @param   string $value
     * @param   array  $data
     * @return string
     */
    public function getAvatarAttr($value, $data)
    {
        return getAvatar($value, $data['nickname']);
    }
    public function getIsEmptyAvatarAttr($value, $data)
    {
        $value = cdnurl($data['avatar'],true);
        if (!$value) {
            return true;
        } else {
            if (substr($value, 0, 4) == 'http') {
                return false;
            } else {
                return true;
            }
        }
        return true;
    }

    /**
     * 获取会员的组别
     */
    public function getGroupAttr($value, $data)
    {
        return UserGroup::get($data['group_id']);
    }

    /**
     * 变更会员余额
     * @param int    $money   余额
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function money($money, $user_id, $memo)
    {
        $user = self::get($user_id);
        if ($user && $money != 0) {
            $before = $user->money;
            $after = $user->money + $money;
            //更新会员信息
            $user->save(['money' => $after]);
            //写入日志
            MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
            return true;
        }
        return false;
    }

    /**
     * 变更会员积分
     * @param int    $score   积分
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function score($score, $user_id, $memo)
    {
        $user = self::get($user_id);
        if ($user && $score != 0) {
            $before = $user->score;
            $after = $user->score + $score;
            // $level = self::nextlevel($after);
            //更新会员信息
            // $user->save(['score' => $after, 'level' => $level]);
            $res1 = $user->save(['score' => $after]);
            //写入日志
            $res2 = ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
            return $res1 && $res2;
        }
        return true;
    }

    /**
     * 根据积分获取等级
     * @param int $score 积分
     * @return int
     */
    public static function nextlevel($score = 0)
    {
        $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
        $level = 1;
        foreach ($lv as $key => $value) {
            if ($score >= $value) {
                $level = $key;
            }
        }
        return $level;
    }
    public function profile()
    {
        return $this->hasOne('UserProfile', 'user_id', 'id', [], 'LEFT');
    }
    public function userprofile()
    {
        return $this->belongsTo('UserProfile', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
    }
    public function extension()
    {
        return $this->hasOne('UserProfile', 'user_id')->bind('is_broker,tags,prov,city,college,service_num,take_look_num,satisfaction_degree,pid,share_qrcode,source_type,user_type,can_loss_state,max_house_state,house_state,broker_user_id,assets_condition,prefer_json,month_login_num,activity_degree,workday_viewhouse,holiday_viewhouse,followtime,msg_open_state,deal_num,pick_num,abandon_num,deal_percent,weigh,enterprise,name,position,phone,performance,employment_time,wechat_img');
    }
}
